//
//  SystemNotificationCell.m
//  HAODE
//
//  Created by LiYinDi on 16/4/15.
//  Copyright © 2016年 reabam.com. All rights reserved.
//

#import "SystemNotificationCell.h"
@interface SystemNotificationCell()
@property(nonnull,nonatomic,strong) UIImageView *iconImageView;
@property(nonnull,nonatomic,strong) UILabel *notiTagLabel;
@property(nonnull,nonatomic,strong) UILabel *messageContentLabel;
@property(nonnull,nonatomic,strong) UILabel *lineLabel;
@property(nonnull,nonatomic,strong) UILabel *timeLabel;
@end
@implementation SystemNotificationCell

- (void)awakeFromNib {
    // Initialization code
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
//        self.backgroundColor = [UIColor colorWithRed:231/255.0 green:231/255.0 blue:231/255.0 alpha:1];
        
       [self createView];
       [self setViewAutoLayout];
    }
    
    return self;

}
-(void)createView
{
    self.iconImageView=[[UIImageView alloc] init];
    [self.iconImageView setImage:[UIImage imageNamed:@"tishi_normal_new.png"]];
//    self.iconImageView.layer.cornerRadius=20;
//    self.iconImageView.layer.masksToBounds=YES;
    [self addSubview:self.iconImageView];
    self.notiTagLabel=[[UILabel alloc] init];
    self.notiTagLabel.text=@"温馨提示";
    self.notiTagLabel.font= [UIFont systemFontOfSize:16];
    self.notiTagLabel.textColor=[UIColor colorWithRed:153.0/255.0f green:153.0/255.0f blue:153.0/255.0f alpha:1.0];
    [self addSubview:self.notiTagLabel];
    
    self.messageContentLabel=[[UILabel alloc] init];
    self.messageContentLabel.numberOfLines=0;
    self.messageContentLabel.font=[UIFont systemFontOfSize:14];
    self.messageContentLabel.textColor=[UIColor colorWithRed:153.0/255.0f green:153.0/255.0f blue:153.0/255.0f alpha:1.0f];
    [self addSubview:self.messageContentLabel];
    
    self.timeLabel=[[UILabel alloc] init];
    self.timeLabel.font=[UIFont systemFontOfSize:11];
    self.timeLabel.textColor=[UIColor colorWithRed:153.0/255.0f green:153.0/255.0f blue:153.0/255.0f alpha:1.0];
    [self addSubview:self.timeLabel];
    
    self.lineLabel=[[UILabel alloc] init];
    [self.lineLabel setBackgroundColor:[UIColor colorWithRed:238.0f/255.0f green:238.0f/255.0f blue:238.0f/255.0f alpha:1.0f]];
    self.lineLabel.textColor=[UIColor clearColor];
    [self addSubview:self.lineLabel];

   }
-(void)setModel:(SystemNotificationModel *)model
{
//    NSString *str=model.messageContent;
    self.messageContentLabel.text=[NSString stringWithFormat:@"%@",model.messageContent];
    NSString *timeStr=model.messageTime;
    NSString *curStr=[self GetCurTime];
    double interval=[self GetStringTimeDiff:timeStr timeE:curStr];
    int hour=interval/3600;
    int minute=(int)interval % 3600 / 60;
    int day=(int)interval/3600/24;
    //    NSLog(@"curStr==%@,leftTimeStr===%@",curStr,leftTimeStr);
    //    NSLog(@"%f",interval);
    //    NSLog(@"hour===%d,minute=%d,day==%d",hour,minute,day);
    if (day!=0&&day>0) {
        self.timeLabel.text=[NSString stringWithFormat:@"%d天前",day];
    }
    else if((day==0||day<0)&&hour>0)
    {
        self.timeLabel.text=[NSString stringWithFormat:@"%d小时前",hour];
    }
    else if ((day<=0&&hour<=0)&&minute>=0) {
        self.timeLabel.text=[NSString stringWithFormat:@"%d分前",minute];
    }
    self.lineLabel.text=[NSString stringWithFormat:@"%@",model.messageContent];
//    self.lineLabel.text=model.messageContent;
//    self.lineLabel.textColor=[UIColor redColor];
    
}

- (double)GetStringTimeDiff:(NSString*)timeS timeE:(NSString*)timeE

{
    
    double timeDiff = 0.0;
    
    NSDateFormatter *formatters = [[NSDateFormatter alloc] init];
    [formatters setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *dateS = [formatters dateFromString:timeS];
    
    
    
    NSDateFormatter *formatterE = [[NSDateFormatter alloc] init];
    [formatterE setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *dateE = [formatterE dateFromString:timeE];
    
    
    timeDiff = [dateE timeIntervalSinceDate:dateS ];
    
    return timeDiff;
    
}

- (NSString*)GetCurTime

{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    NSString*timeString=[formatter stringFromDate: [NSDate date]];
    
    
    return timeString;
}


-(void)setViewAutoLayout
{
    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).with.offset(10);
        make.width.equalTo(@30);
        
        make.height.equalTo(@30);
        
        make.top.equalTo(self.mas_top).with.offset(24);
    }];
    [self.notiTagLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconImageView.mas_right) ;
        make.right.equalTo(self.mas_right).with.offset(10);
        
        make.height.equalTo(@21);
        
        make.centerY.equalTo(self.iconImageView.mas_centerY) ;
    }];
    [self.messageContentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).with.offset(23) ;
        make.right.equalTo(self.mas_right).with.offset(-10);
        
        make.bottom.equalTo(_timeLabel.mas_top).with.offset(-15);
        
        make.top.equalTo(_notiTagLabel.mas_bottom).with.offset(14) ;
    }];
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).with.offset(23);
        make.right.equalTo(self.mas_right);
//        make.bottom.equalTo(self.lineLabel.mas_top).with.offset(-10);
        make.height.equalTo(@18);
        
        make.top.equalTo(self.messageContentLabel.mas_bottom).with.offset(15) ;
    }];
    [self.lineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).with.offset(10);
        make.right.equalTo(self.mas_right).with.offset(-10) ;
        
        make.height.equalTo(@1);
        
        make.top.equalTo(self.timeLabel.mas_bottom).with.offset(7);
    }];
    
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
